package cn.llssit.controller;

import cn.llssit.common.utils.StringUtils;
import cn.llssit.domain.MovieSearch;
import cn.llssit.framework.web.domain.AjaxResult;
import cn.llssit.mapper.MovieMapper;
import cn.llssit.mapper.MovieSearchMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

/**
 * @Author: Lesan
 * @Date: 2020-06-24
 */
@RestController
@RequestMapping("/esSearch")
public class ElasticsearchController {

    @Autowired
    MovieSearchMapper movieSearchMapper;

    @Autowired
    MovieMapper movieMapper;

    @ApiOperation("")
    @RequestMapping(value = "/importAll", method = RequestMethod.GET)
    public AjaxResult importAllList() {
        List<MovieSearch> movies = movieMapper.selectAllForSearch();
        Iterable<MovieSearch> movieSearchIterable = movieSearchMapper.saveAll(movies);
        Iterator<MovieSearch> iterator = movieSearchIterable.iterator();
        int result = 0;
        while (iterator.hasNext()) {
            result++;
            iterator.next();
        }
        return AjaxResult.success(result);
    }

    @ApiOperation("")
    @DeleteMapping("/deleteAll")
    public AjaxResult deleteAll() {
        movieSearchMapper.deleteAll();
        return AjaxResult.success();
    }

    @ApiOperation("")
    @GetMapping("/search/simple")
    public AjaxResult Search(String keyword) {
        if (StringUtils.isEmpty(keyword))
            return AjaxResult.error();
        return AjaxResult.success(movieSearchMapper.findByName(keyword));
    }

    /*
    @PostMapping("/add")
    public ResponseEntity<Object> add(@RequestBody BlogModel blogModel) {
        blogDao.save(blogModel);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/get/{id}")
    public ResponseEntity<Object> getById(@PathVariable String id) {
        if (StringUtils.isEmpty(id))
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        Optional<BlogModel> blogModelOptional = blogDao.findById(id);
        if (blogModelOptional.isPresent()) {
            BlogModel blogModel = blogModelOptional.get();
            return new ResponseEntity<>(blogModel,HttpStatus.OK);
        }
        return new  ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    @GetMapping("/rep/search/title")
    public ResponseEntity<Object> repSearchTitle(String keyword) {
        if (StringUtils.isEmpty(keyword))
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        return new ResponseEntity<>(blogDao.findByTitleLike(keyword),HttpStatus.OK);
    }

    @DeleteMapping("/delete")
    public ResponseEntity<Object> deleteById() {
        blogDao.deleteAll();
        return new ResponseEntity<>(HttpStatus.OK);
    }
*/
}
